package com.lemon.smartsafety.utils.app;

import android.content.Context;
import android.content.SharedPreferences;

import com.lemon.smartsafety.BuildConfig;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

/**
 * Created by Cooliean on 14-6-8.
 * SharedPreference 工具类
 */
public class SharedPreferencesServer {
    private LoggerHelper loggerHelper = new LoggerHelper(SharedPreferencesServer.class);

    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;


    /**
     * 实例化 SharedPreferenceHelper 用默认的名字 app_share
     *
     * @param context 当前上下文
     */
    public SharedPreferencesServer(Context context) {
        mSharedPreferences = context.getSharedPreferences(Constants.SHAREPATH1, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
        initServerUrls();
    }

    /**
     * 实例化 SharedPreferenceHelper 用自定义名字
     *
     * @param context       当前上下文
     * @param shareFileName 自定义文件名
     */
    public SharedPreferencesServer(Context context, String shareFileName) {
        mSharedPreferences = context.getSharedPreferences(shareFileName, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
        initServerUrls();
    }

    private void initServerUrls() {

        //changed by Andrew.yin 2017/10/11 begin  保存最新的URL地址

        if(BuildConfig.FUNCTION_DEBUG) {
            //内部测试版
            if (StringUtils.isEmpty(getHttpBaseHost())) {
                setHttpBaseHost(Constants.DEFAULT_KEY_CL_HTTP_BASE_URL);
            }
            if (StringUtils.isEmpty(getHttpBaseHostPort())) {
                setHttpBaseHostPort(Constants.DEFAULT_KEY_CL_HTTP_PORT);
            }
            if (StringUtils.isEmpty(getHttpBaseHostVD())) {
                setHttpBaseHostVD(Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY);
            }


            if (StringUtils.isEmpty(getFileBaseHost())) {
                setFileBaseHost(Constants.DEFAULT_KEY_CL_FILES_BASE_URL);
            }

            if (StringUtils.isEmpty(getFileBasePort())) {
                setFileBasePort(Constants.DEFAULT_KEY_CL_FILES_PORT);
            }

            if (StringUtils.isEmpty(getFileBaseVD())) {
                setFileBaseVD(Constants.DEFAULT_KEY_CL_FILES_VIRTUAL_DIRECTORY);
            }
        } else {
            //发布版
            if (!Constants.DEFAULT_KEY_CL_HTTP_BASE_URL.equals(getHttpBaseHost())) {
                setHttpBaseHost(Constants.DEFAULT_KEY_CL_HTTP_BASE_URL);
            }
            if (!Constants.DEFAULT_KEY_CL_HTTP_PORT.equals(getHttpBaseHostPort())) {
                setHttpBaseHostPort(Constants.DEFAULT_KEY_CL_HTTP_PORT);
            }
            if (!Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY.equals(getHttpBaseHostVD())) {
                setHttpBaseHostVD(Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY);
            }


            if (!Constants.DEFAULT_KEY_CL_FILES_BASE_URL.equals(getFileBaseHost())) {
                setFileBaseHost(Constants.DEFAULT_KEY_CL_FILES_BASE_URL);
            }

            if (!Constants.DEFAULT_KEY_CL_FILES_PORT.equals(getFileBasePort())) {
                setFileBasePort(Constants.DEFAULT_KEY_CL_FILES_PORT);
            }

            if (!Constants.DEFAULT_KEY_CL_FILES_VIRTUAL_DIRECTORY.equals(getFileBaseVD())) {
                setFileBaseVD(Constants.DEFAULT_KEY_CL_FILES_VIRTUAL_DIRECTORY);
            }
        }

        //changed by Andrew.yin 2017/10/11 end
    }


    public SharedPreferencesServer setHttpBaseHost(String host) {
        mEditor.putString(Constants.KEY_CL_HTTP_BASE_URL, host);
        mEditor.commit();
        return this;
    }

    public String getHttpBaseHost() {
        return getSPString(Constants.KEY_CL_HTTP_BASE_URL);
    }


    public SharedPreferencesServer setHttpBaseHostPort(String string) {
        mEditor.putString(Constants.KEY_CL_HTTP_PORT, string);
        mEditor.commit();
        return this;
    }

    public String getHttpBaseHostPort() {
        return getSPString(Constants.KEY_CL_HTTP_PORT);
    }


    public SharedPreferencesServer setHttpBaseHostVD(String string) {
        mEditor.putString(Constants.KEY_CL_HTTP_VIRTUAL_DIRECTORY, string);
        mEditor.commit();
        return this;
    }

    public String getHttpBaseHostVD() {
        return getSPString(Constants.KEY_CL_HTTP_VIRTUAL_DIRECTORY);
    }


    public SharedPreferencesServer setFileBaseHost(String string) {
        mEditor.putString(Constants.KEY_CL_FILES_BASE_URL, string);
        mEditor.commit();
        return this;
    }

    public String getFileBaseHost() {
        return getSPString(Constants.KEY_CL_FILES_BASE_URL);
    }


    public SharedPreferencesServer setFileBasePort(String string) {
        mEditor.putString(Constants.KEY_CL_FILES_PORT, string);
        mEditor.commit();
        return this;
    }

    public String getFileBasePort() {
        return getSPString(Constants.KEY_CL_FILES_PORT);
    }


    public SharedPreferencesServer setFileBaseVD(String string) {
        mEditor.putString(Constants.KEY_CL_FILES_VIRTUAL_DIRECTORY, string);
        mEditor.commit();
        return this;
    }

    public String getFileBaseVD() {
        return getSPString(Constants.KEY_CL_FILES_VIRTUAL_DIRECTORY);
    }

    public String getSPString(String key) {
        return getSPString(key, "");
    }

    public String getSPString(String key, String defaultValue) {
        return StringUtils.nullValue(mSharedPreferences.getString(key, defaultValue));
    }


    public String getHttpUrl() {
        //changed by Andrew.yin 2017/7/20 begin. 我们使用静态服务器地址
        return String.format("http://%s:%s/%s", getSPString(Constants.KEY_CL_HTTP_BASE_URL, ""), getSPString(Constants.KEY_CL_HTTP_PORT, ""), getSPString(Constants.KEY_CL_HTTP_VIRTUAL_DIRECTORY, ""));
        //return String.format("http://%s:%s/%s", Constants.DEFAULT_KEY_CL_HTTP_BASE_URL, Constants.DEFAULT_KEY_CL_HTTP_PORT, Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY);
        //changed by Andrew.yin 2017/7/20 end
    }

    public String getFilesUrl() {
        //changed by Andrew.yin 2017/7/20 begin. 我们使用静态服务器地址
        return String.format("http://%s:%s/%s", getSPString(Constants.KEY_CL_FILES_BASE_URL, ""), getSPString(Constants.KEY_CL_FILES_PORT, ""), getSPString(Constants.KEY_CL_FILES_VIRTUAL_DIRECTORY, ""));
       // return String.format("http://%s:%s/", getSPString(Constants.KEY_CL_FILES_BASE_URL, ""), getSPString(Constants.KEY_CL_FILES_PORT, ""));
        //return String.format("http://%s:%s/%s", Constants.DEFAULT_KEY_CL_FILES_BASE_URL, Constants.DEFAULT_KEY_CL_FILES_PORT, Constants.DEFAULT_KEY_CL_FILES_VIRTUAL_DIRECTORY);
        //changed by Andrew.yin 2017/7/20 end.
    }


    /**
     * 获取网络请求的完整地址
     *
     * @param url 要拼接的地址
     * @return
     */
    public String getRequestUrl(String url) {
        String formatString = "%s%s";
        if (!url.startsWith("/")) {
            formatString = "%s/%s";
        }
        return String.format(formatString, getHttpUrl(), url);
    }

    //add by Andrew.yin 2017/8/15 begin. 迭代旧的服务端

    /**
     * 获取新的网络请求的完整地址
     *
     * @param url 要拼接的地址
     * @return
     */
    public String getRequestUrl(String url, String server_version) {
        String formatString = "%s%s";
        if (!url.startsWith("/")) {
            formatString = "%s/%s";
        }
        if (Constants.SERVER_V2.equals(server_version)) {
            return String.format(formatString, getHttpUrl(server_version), url);
        } else if (Constants.MONITOR_V1.equals(server_version)) {
            return String.format(formatString, getHttpUrl(server_version), url);
        } else if (Constants.GSAFE.equals(server_version)) {
            return String.format(formatString, getHttpUrl(server_version), url);
        } else if (Constants.VIDEO_SERVER.equals(server_version)) {
            return String.format(formatString, getHttpUrl(server_version), url);
        } else {
            return String.format(formatString, getHttpUrl(), url);
        }
    }

    private String getHttpUrl(String server_version) {
        //TODO 这里用String的方式，将来可以处理，多种后台版本的迭代
        //changed by Andrew.yin 2017/7/20 begin. 我们使用静态服务器地址
        if (Constants.SERVER_V2.equals(server_version)) {
            // ====== Constants.SERVER_V2 acloud_new ===========
            return String.format("http://%s:%s/%s",
                    getSPString(Constants.KEY_CL_HTTP_BASE_URL, ""),
                    getSPString(Constants.KEY_CL_HTTP_PORT, ""),
                    Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY_NEW);

        } else if(Constants.MONITOR_V1.equals(server_version)){
            // ====== Constants.MONITOR_V1 实时监测 ===========
            //return String.format("http://%s:%s/%s", getSPString(Constants.KEY_CL_HTTP_BASE_URL, ""), getSPString(Constants.KEY_CL_HTTP_PORT, ""), Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY_MONITOR);

            return String.format("http://%s:%s/%s", "58.215.171.233", "18080",
                    Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY_MONITOR);

        } else if(Constants.VIDEO_SERVER.equals(server_version)){
            // ====== Constants.VIDEO_SERVER 视频监控 ===========

            return String.format("http://%s:%s/%s", "58.215.171.233", "18080",
                    Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY_NEW);

        } else {
            // ====== Constants.GSAFE ===========

            return String.format("http://%s:%s/%s",
                    getSPString(Constants.KEY_CL_HTTP_BASE_URL, ""),
                    getSPString(Constants.KEY_CL_HTTP_PORT, ""),
                    Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY_GSAFE);
        }
        //return String.format("http://%s:%s/%s", Constants.DEFAULT_KEY_CL_HTTP_BASE_URL, Constants.DEFAULT_KEY_CL_HTTP_PORT, Constants.DEFAULT_KEY_CL_HTTP_VIRTUAL_DIRECTORY);
        //changed by Andrew.yin 2017/7/20 end
    }
    //add by Andrew.yin 2017/8/15 end


    /**
     * 清除SharedPreferences
     */
    public SharedPreferencesServer clear() {
        mEditor.clear();
        mEditor.commit();
        return this;
    }
}
